<?php

// src/Acme/StoreBundle/Controller/UserController.php
namespace Acme\StoreBundle\Controller\Admin;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use \Acme\StoreBundle\Form\Admin\UserType;
use Symfony\Component\HttpFoundation\Request;
use Acme\StoreBundle\Entity\User;

class UserController extends Controller
{
    public function addAction() {
        
        $user = new User();
        $form = $this->createForm( new UserType(), $user, array(
            'action' => $this->generateUrl('admin_user_create')
        ) );
        
        return $this->render(
            'StoreBundle:Admin\User:add.html.twig',
            array('form' => $form->createView())
        );
    }
    
    public function createAction( Request $request ) {
        $em = $this->getDoctrine()->getManager();
        $user = new User();
        $form = $this->createForm(new UserType(), $user);

        $form->handleRequest($request);
        
        if ($form->isValid()) {
            
            $form_data = $form->getData();
            $user->setUsername($form_data->getUsername());
            $user->setEmail($form_data->getEmail());
            $user->setPassword($form_data->getPassword());
            $em->persist($user);
            $em->flush();

            return $this->redirect( $this->generateUrl( 'admin_user_add' ) );
        }

        return $this->render(
            'StoreBundle:Admin\User:add.html.twig',
            array('form' => $form->createView())
        );
    }
    
    public function indexAction() {
        
        $repository = $this->getDoctrine()
            ->getRepository('StoreBundle:User');
        $users = $repository->findAll();
        
        
        return $this->render(
            'StoreBundle:Admin\User:index.html.twig',
            array ('users' => $users)
        );
    }
}